<template>
  <!-- 动态数据源配置 -->
  <div>
    <el-form-item label="数据类型">
      <el-radio-group v-model="data.type">
        <el-radio-button label="static">静态数据</el-radio-button>
        <el-radio-button label="dynamic">动态数据</el-radio-button>
      </el-radio-group>
    </el-form-item>
    <template v-if="data.type=='static'">
      {{data}}
      <!-- 树形结构增删改 -->
      <HuTreeNode v-model="data.value" v-if="data.valueType=='tree'" />
      <!-- 列表结构增删改 -->
      <HuOptions :options.sync="data.value" v-else-if="data.valueType=='list'" />
      <el-form-item label="数据内容" v-else>
        <el-button type="primary" plain @click="$refs.JsonDialog.open()">编辑数据</el-button>
        <JsonDialog ref="JsonDialog" v-model="data.value" />
      </el-form-item>
    </template>

    <el-form-item label="数据内容" v-if="data.type=='dynamic'">
      <el-button plain type="primary" @click="$refs.DataDialog.open()">编辑数据</el-button>
      <DataDialog ref="DataDialog" v-model="data" />
    </el-form-item>

    <template v-if="data.props">
      <el-form-item label="标签键名" v-if="data.props.label !== undefined">
        <el-input v-model="data.props.label" placeholder="请输入标签键名" />
      </el-form-item>
      <el-form-item label="值键名" v-if="data.props.value !== undefined">
        <el-input v-model="data.props.value" placeholder="请输入值键名" />
      </el-form-item>
      <el-form-item label="子级键名" v-if="data.props.children !== undefined">
        <el-input v-model="data.props.children" placeholder="请输入子级键名" />
      </el-form-item>
      <template v-if="data.props &&data.props.request">
        <el-divider>请求数据</el-divider>
        <el-form-item v-for="(value, key) in data.props.request" :key="key" :label="key + ' 键名'">
          <el-input v-model="data.props.request[key]" />
        </el-form-item>
      </template>
      <template v-if="data.props &&data.props.response">
        <el-divider>响应数据</el-divider>
        <el-form-item v-for="(value, key) in data.props.response" :key="key" :label="key + ' 键名'">
          <el-input v-model="data.props.response[key]" />
        </el-form-item>
      </template>
    </template>
    <el-form-item v-if="data.head !== undefined" label="表头数据">
      <el-button type="primary" plain size="small" @click="$refs.TableHead.open()">编辑数据</el-button>
    </el-form-item>
    <TableHead v-model="data.head" ref="TableHead" />
  </div>
</template>

<script>
import { deepClone } from "../utils";
import DataDialog from "./DataDialog";
import HuTreeNode from "../huComponents/HuTreeNode.vue";
import HuOptions from "../huComponents/HuOptions.vue";
import JsonDialog from "@/components/editor/JsonDialog";
import TableHead from "./TableHead";

export default {
  name: "DataConfig",
  components: {
    HuTreeNode,
    DataDialog,
    JsonDialog,
    TableHead,
    HuOptions,
  },
  props: {
    value: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      data: {},
    };
  },
  watch: {
    data: {
      immediate: true,
      deep: true,
      handler(newValue) {
        if (newValue) {
          this.$emit("input", newValue);
        }
      },
    },
    value: {
      immediate: true,
      deep: true,
      handler(newValue) {
        if (JSON.stringify(newValue) !== JSON.stringify(this.data)) {
          this.data = deepClone(this.value);
        }
      },
    },
  },
  methods: {},
};
</script>

<style lang="scss" >
</style>